<html>

<head>
    <meta charset='utf-8'>
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <!-- 可选的Bootstrap主题文件（一般不用引入） -->
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="http://cdn.bootcss.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script>
    var addTab = function(obj) {
        var id = "tab_" + obj.id;
        $(".active").removeClass("active");
        //如果TAB不存在，创建一个新的TAB
        if (!$("#" + id)[0]) {
            //固定TAB中IFRAME高度
            mainHeight = 100;
            //创建新TAB的title
            title = '<li role="presentation" id="tab_' + id + '"><a href="#' + id + '" aria-controls="' + id + '" role="tab" data-toggle="tab">' + obj.title;
            //是否允许关闭
            if (obj.close) {
                title += ' <i class="tabclose" tabclose="' + id + '"><span class="glyphicon glyphicon-remove"></span></i>';
            }
            title += '</a></li>';
            //是否指定TAB内容
            if (obj.content) {
                content = '<div role="tabpanel" class="tab-pane" id="' + id + '">' + obj.content + '</div>';
            } else { //没有内容，使用IFRAME打开链接
                content = '<div role="tabpanel" height="100%" width="100%" class="tab-pane" id="' + id + '"><iframe src="' + obj.url + '" width="100%" height="' + mainHeight +
                    '%" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes"></iframe></div>';
            }
            //加入TABS
            $(".nav-tabs").append(title);
            $(".tab-content").append(content);
        }
        //激活TAB
        $("#tab_" + id).addClass('active');
        $("#" + id).addClass("active");
    };

    var closeTab = function(id) {
        //如果关闭的是当前激活的TAB，激活他的前一个TAB
        if ($("li.active").attr('id') == "tab_" + id) {
            $("#tab_" + id).prev().addClass('active');
            $("#" + id).prev().addClass('active');
        }
        //关闭TAB
        $("#tab_" + id).remove();
        $("#" + id).remove();
    };
    $(function() {
        mainHeight = $(document.body).height() - 45;
        $('.main-left,.main-right').height(mainHeight);
        $(".nav-tabs").on("click", "[tabclose]", function(e) {
            id = $(this).attr("tabclose");
            closeTab(id);
        });
        $('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
            console.log($(e.target));
        })
    });
    </script>
    <script>
    $(function() {
        $("#addnew").on("click", function() {
            addTab({
                id: 'alex',
                title: 'alexchen',
                close: true,
                content: 'http://www.baidu.com'
            });
        })
    });
    </script>
</head>

<body>
    <div role="tabpanel">
        <!-- Nav tabs -->
        <ul class="nav nav-tabs" role="tablist" id="myTab">
            <li role="presentation" class="active">
                <a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>
            <li role="presentation">
                <a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
            <li role="presentation">
                <a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a></li>
            <li role="presentation">
                <a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
        </ul>
        <!-- Tab panes -->
        <div class="tab-content">
            <div role="tabpanel" class="tab-pane" id="home">home</div>
            <div role="tabpanel" class="tab-pane" id="profile">profile</div>
            <div role="tabpanel" class="tab-pane" id="messages">messages</div>
            <div role="tabpanel" class="tab-pane" id="settings">settings</div>
        </div>
    </div>
</body>

</html>


